import { createRouter, createWebHashHistory, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
// import
const routerHistory = createWebHashHistory()
// createWebHashHistory hash 路由
// createWebHistory history 路由
// createMemoryHistory 带缓存 history 路由

// const login = () => import('../views/login/index.vue')
const routers = useRoute
const app = () => import('../views/app.vue')
const router = createRouter({
  history: routerHistory,
  routes: [
    {
      path: '/',
      component: app,
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'home',
          component: () => import('../views/home/index.vue'),
        },
        {
          path: '/wrongPage',
          name: 'wrongPage',
          component: () => import('../views/wrongPage/index.vue'),
        },
        {
          path: '/job',
          name: 'job',
          component: () => import('../views/job/index.vue'),
        },
        {
          path: '/order',
          name: 'order',
          component: () => import('../views/order/order.vue'),
        },
        {
          path: '/portfolio',
          name: 'portfolio',
          component: () => import('../views/portfolio/index.vue'),
        },
      ],
    },
    { path: '/login', component: () => import('../views/login/index.vue') },
  ],
})

console.log(router)
const error = () => {
  ElMessage.error('请登录')
}
router.beforeEach((to, from, next) => {
  if (!localStorage.getItem('token')) {
    if (to.path === '/login') {
      next()
    } else {
      if (from.fullPath) {
        error()
        next('/login?Return=' + from.fullPath)
      }
    }
  } else {
    next()
  }
})
// 删除/重置路由
// export function resetRoute(): void {
//   router.getRoutes().forEach((route) => {
//     const { name } = route
//     if (name) {
//       router.hasRoute(name) && router.removeRoute(name)
//     }
//   })
// }

// export function setupRouter(app: App<Element>): void {
//   app.use(router)
// }
export default router
